<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增产品')" />
	<th:block th:include="include :: bootstrap-fileinput-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-product-add">
        	<input name="supplierId" type="hidden" id="supplierId" />
        	<input name="productCatogoryId" type="hidden" id="treeId"/>
            <div class="form-group">    
                <label class="col-sm-3 control-label">供应商名称：</label>
                <div class="col-sm-8">
                	<div class="input-group">
	                    <input name="supplierName" onclick="selectSupplier()" id="supplierName" type="text" placeholder="请选择供应商 " class="form-control" readonly>
	                    <span class="input-group-addon"><i class="fa fa-search"></i></span>
                    </div>                    
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">产品分类：</label>
                <div class="col-sm-8">
                	<div class="input-group">
	                    <input name="catogoryName" onclick="selectCatogory()" id="treeName" type="text" placeholder="请选择产品分类 " class="form-control" required readonly>
	                    <span class="input-group-addon"><i class="fa fa-search"></i></span>
                    </div>                    
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">产品名称：</label>
                <div class="col-sm-8">
                    <input name="productName" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">进价(元)：</label>
                <div class="col-sm-8">
                    <input name="incomePrice" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">售价(元)：</label>
                <div class="col-sm-8">
                    <input name="salePrice" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">产品类别：</label>
                <div class="col-sm-8">
                    <select name="productCategory" class="form-control m-b" th:with="type=${@dict.getType('product_category')}" required>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">货号：</label>
                <div class="col-sm-8">
                    <input name="productNo" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">计量单位：</label>
                <div class="col-sm-8">
                    <select name="productUnit" class="form-control m-b" th:with="type=${@dict.getType('product_unit')}" required>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">产品状态：</label>
                <div class="col-sm-8">
                    <div class="radio-box" th:each="dict : ${@dict.getType('product_status')}">
                        <input type="radio" th:id="${'prodcutStatus_' + dict.dictCode}" name="prodcutStatus" th:value="${dict.dictValue}" th:checked="${dict.default}" required>
                        <label th:for="${'prodcutStatus_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">产品图片：</label>
                <div class="col-sm-8">                	
                	<img class="img-lg" id="productImageView">
                    <input type="hidden" id="productImage" name="productImage">
                    <div class="file-loading">
                        <input class="form-control file-upload" id="productImageUpload" name="file" type="file">
                    </div>
                </div>
            </div>
            <div class="form-group">
						<label class="col-xs-3 control-label">备注：</label>
						<div class="col-xs-8">
							<textarea name="remark" maxlength="500" class="form-control"
								rows="3"></textarea>
						</div>
			</div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <script type="text/javascript">
        var prefix = ctx + "crm/product"
        $("#form-product-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-product-add').serialize());
            }
        }
        
        /* 产品管理-新增-选择供应商  */
        function selectSupplier() {
        	 var url = ctx + "crm/supplier/selectSupplier";
        	 $.modal.open("选择供应商", url);
		}
        
        /*产品管理-新增-选择产品分类树*/
        function selectCatogory() {
        	var treeId = $("#treeId").val();
        	var productCatogoryId = $.common.isEmpty(treeId) ? "100" : $("#treeId").val();
        	var url = ctx + "crm/productCatogory/selectProductCatogoryTree/" + productCatogoryId;
			var options = {
				title: '选择产品分类',
				width: "380",
				url: url,
				callBack: doSubmit
			};
			$.modal.openOptions(options);
		}
		
		function doSubmit(index, layero){
			var tree = layero.find("iframe")[0].contentWindow.$._tree;
			if ($.tree.notAllowParents(tree)) {
				var body = layer.getChildFrame('body', index);
    			$("#treeId").val(body.find('#treeId').val());
    			$("#treeName").val(body.find('#treeName').val());
    			layer.close(index);
			}
		}

	    $("#productImageUpload").fileinput({
	        uploadUrl: ctx + 'common/upload',
	        showPreview: false,
	        autoReplace: true,
	        allowedFileExtensions: ['jpg', 'gif', 'png', 'jpeg', 'pic', 'bmp'],//接收的文件后缀
	    }).on('fileuploaded', function (event, data, previewId, index) {
	        if (data == undefined) {
	            $.modal.alertWarning('文件格式类型不正确');
	            return;
	        }	        
	        $("#productImage").val(data.response.fileName);
	        $("#productImageView").attr('src',data.response.url);
	    }).on('fileremoved', function (event, id, index) {
	        $("#productImage").val('')
	    })

    </script>
</body>
</html>